package com.ztmg.water.service.water.impl;

import com.ztmg.water.model.water.WaterTicket;
import com.ztmg.water.vo.water.WaterTicketListVO;
import com.ztmg.water.vo.water.WaterTicketVO;
import com.ztmg.water.dto.water.WaterTicketDTO;
import com.ztmg.water.dto.water.WaterTicketPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.water.WaterTicketService;
import com.ztmg.water.mapper.water.WaterTicketMapper;import com.ztmg.water.model.water.WaterTicketExample;

@Service
public class WaterTicketServiceImpl implements WaterTicketService{

    @Resource
    private WaterTicketMapper waterTicketMapper;
    @Override
    public JsonResult create(WaterTicketDTO dto) {
        if(null != dto){
            WaterTicket bean = new WaterTicket();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            waterTicketMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(WaterTicketDTO dto) {
        if(null != dto && null != dto.getId()){
            WaterTicket bean = new WaterTicket();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            waterTicketMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<WaterTicketVO> selectById(Integer id) {
        if(null != id){
            WaterTicket bean = waterTicketMapper.selectByPrimaryKey(id);
            WaterTicketVO vo = new WaterTicketVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<List<WaterTicketVO>> select(WaterTicketDTO dto) {
        WaterTicketExample example = new WaterTicketExample();
        WaterTicketExample.Criteria criteria =  example.createCriteria();
        if(dto.getTicketType()!=null)
        {
            criteria.andTicketTypeEqualTo(dto.getTicketType());
        }
        if(dto.getIsSwap()!=null)
        {
            criteria.andIsSwapEqualTo(dto.getIsSwap());
        }
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<WaterTicket> beanList = waterTicketMapper.selectByExample(example);
        List<WaterTicketVO> respDTOS = new ArrayList<>();
        if(!beanList.isEmpty()){
            respDTOS = beanList.parallelStream().map((bean) -> {
                WaterTicketVO vo = new WaterTicketVO();
                BeanUtils.copyProperties(bean, vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return JsonResult.success(respDTOS);
    }

    @Override
    public JsonResult<List<WaterTicketVO>> listAll(WaterTicketDTO dto) {

        WaterTicketExample example = new WaterTicketExample();
        WaterTicketExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(1);
        List<WaterTicket> beanList = waterTicketMapper.selectByExample(example);
        List<WaterTicketVO> respDTOS = new ArrayList<>();
        if(!beanList.isEmpty()){
            respDTOS = beanList.parallelStream().map((bean) -> {
                WaterTicketVO vo = new WaterTicketVO();
                BeanUtils.copyProperties(bean, vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return JsonResult.success(respDTOS);
    }

    @Override
    public JsonResult<PageInfo<WaterTicketVO>> selectPage(WaterTicketPageDTO dto) {
        WaterTicketExample example = new WaterTicketExample();
        WaterTicketExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<WaterTicket> beanList = waterTicketMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<WaterTicketVO> respDTOS = beanList.parallelStream().map((bean) -> {
            WaterTicketVO vo = new WaterTicketVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult<List<WaterTicketListVO>> selectAllListPage() {
        return null;
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( waterTicketMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(WaterTicketDTO dto) {
        WaterTicketExample example = new WaterTicketExample();
        WaterTicketExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(waterTicketMapper.deleteByExample(example));
    }
}